import uuid
from django.db.models.fields import CharField
import shortuuid


def uuid1_hex():
    """
    return uuid1 hex string

    eg: 23f87b528d0f11e696a7f45c89a84eed
    """
    return uuid.uuid1()


class ShortUUIDField(CharField):
    """
    A field which stores a Short UUID value in base57 format. This may also have
    the Boolean attribute 'auto' which will set the value on initial save to a
    new UUID value (calculated using shortuuid's default (uuid4)). Note that while all
    UUIDs are expected to be unique we enforce this with a DB constraint.
    """
    def pre_save(self, model_instance, add):
        """
        This is used to ensure that we auto-set values if required.
        See CharField.pre_save
        """
        value = super(ShortUUIDField, self).pre_save(model_instance, add)
        if not value:
            value = shortuuid.ShortUUID().random(length=self.max_length)
            setattr(model_instance, self.attname, value)
        return value

    # def formfield(self, **kwargs):
    #     if self.auto:
    #         return None
    #     return super(ShortUUIDField, self).formfield(**kwargs)



